package com.yanqu.road.server.cmd.activity.jiuzhou;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.jiuzhou.CrossJiuZhouProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.manager.activity.jiuzhou.CrossJiuZhouMgr;
import com.yanqu.road.server.manager.activity.jiuzhou.activity.CrossJiuZhouActivity;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_CROSS_JIU_ZHOU_LOGIN, desc = "跨服登录")
public class CrossJiuZhouLoginCmd extends CrossCommand {

    private int activityId;

    private PlayerProto.PlayerBaseTempMsg playerBaseTempMsg;

    private long playerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossJiuZhouProto.CrossJiuZhouLoginReqMsg reqMsg = CrossJiuZhouProto.CrossJiuZhouLoginReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        playerBaseTempMsg = reqMsg.getUserBaseInfo();
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossJiuZhouActivity activity = CrossJiuZhouMgr.getActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_JIU_ZHOU_ACTIVITY_NOT_EXIST;
            return;
        }
        // 更新userBaseInfo
        if (playerBaseTempMsg != null) {
            UserBaseInfo userBaseInfo = PlayerBasePb.parseBaseUserInfo(playerBaseTempMsg);
            CrossUserMgr.updateUserBaseInfo(playerId, userBaseInfo);
        }

        activity.crossLoginByCmd(playerId);
    }

    @Override
    public void sendData() {

    }
}
